﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2015-20XX，My                                                *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019/11/26 23:23:35
 *  文件描述:逻辑类
 *****************************************************************************/

using StateModel;
using UnityEngine;
using UnityEngine.UI;
using Util;

public class UiMainUiLogic : MonoBehaviour
{
    private UiMainUiInfo m_window;

    private void Awake()
    {
        OnInit();
        OnOpen();
    }

    protected void OnInit()
    {
        m_window = new UiMainUiInfo(transform);
    }

    protected void OnOpen()
    {
        AddListener();
    }

    protected void OnClose()
    {
        RemoveListener();
    }

    private void AddListener()
    {
    }

    private void RemoveListener()
    {
    }

    private Controller SelectedObj;
    private SCV scv;

    private void Update()
    {
        if (SelectInBarracks) {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            var pos = ray.origin.toVector2() - SelectInBarracks.boxCollider.offset;
            var size = SelectInBarracks.boxCollider.size;
            debug.DrawBox(pos, size, Color.gray);
            if (Input.GetMouseButtonDown(0)) {//选定建造位置
                var hit = Physics2D.BoxCast(pos, size, 0, Vector3.forward);
                if (!hit) {
                    var build = scv.m_StateManage.Fin<ScvBuildState>(StateType.Build);
                    build.Build(SelectInBarracks._Id, pos);
                }
                SelectInBarracks = null;
            }
        }
        else
        if (Input.GetMouseButtonDown(0)) {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            var hit = Physics2D.CircleCast(ray.origin, 0.2f, Vector3.forward);
            if (hit) {
                debug.log(hit.transform.name);
                var con = hit.transform.GetComponent<Controller>();
                if (con) {
                    SelectedObj = con;
                    SetInfo(SelectedObj);
                    switch (SelectedObj._Type) {
                        case ControllerType.Barracks: {
                                var b = SelectedObj as Barracks;
                                if (b.m_StateManage.curState.stateType == StateModel.StateType.Drill) {
                                    m_window.m_create.SetActive(true);
                                }
                                else {
                                    m_window.m_create.SetActive(false);
                                }
                                var drill = b.m_StateManage.Fin<BarracksDrillState>(StateType.Drill);

                                var list = drill.soldierAll;
                                m_window.m_menuButton.gameObject.SetActive(false);

                                for (int i = 0; i < list.Length; i++) {
                                    var soldier = list[i];
                                    Button btn = null;
                                    if (m_window.m_menu.transform.childCount <= i) {
                                        btn = GameObject.Instantiate(m_window.m_menuButton);
                                        btn.transform.parent = m_window.m_menu.transform;
                                        btn.transform.localScale = Vector3.one;
                                    }
                                    else {
                                        btn = m_window.m_menu.transform.GetChild(i).GetComponent<Button>();
                                    }
                                    btn.gameObject.SetActive(true);

                                    btn.transform.GetComponentInChildren<Text>().text = soldier._Name;
                                    btn.onClick.RemoveAllListeners();
                                    btn.onClick.AddListener(() =>
                                    {
                                        if (b.m_input.data.Execution.Count < 5) {
                                            b.m_input.data.Execution.Add(new Instruct.Order(soldier._Id));
                                        }
                                    });
                                }
                            }
                            break;

                        case ControllerType.Soldier:
                            break;

                        case ControllerType.SCV: {
                                SCV b = SelectedObj as SCV;
                                scv = b;
                                m_window.m_create.SetActive(false);
                                var Build = b.m_StateManage.Fin<ScvBuildState>(StateType.Build);

                                var list = Build.barracksAll;
                                m_window.m_menuButton.gameObject.SetActive(false);

                                for (int i = 0; i < list.Length; i++) {
                                    var soldier = list[i];
                                    Button btn = null;
                                    if (m_window.m_menu.transform.childCount <= i) {
                                        btn = GameObject.Instantiate(m_window.m_menuButton);
                                        btn.transform.parent = m_window.m_menu.transform;
                                        btn.transform.localScale = Vector3.one;
                                    }
                                    else {
                                        btn = m_window.m_menu.transform.GetChild(i).GetComponent<Button>();
                                    }
                                    btn.gameObject.SetActive(true);

                                    btn.transform.GetComponentInChildren<Text>().text = soldier._Name;
                                    btn.onClick.RemoveAllListeners();
                                    btn.onClick.AddListener(() =>
                                    {
                                        SelectInBarracks = soldier;
                                    });
                                }
                            }
                            break;
                    }
                }
            }
        }

        if (SelectedObj) {
            switch (SelectedObj._Type) {
                case ControllerType.Barracks:
                    var b = SelectedObj as Barracks;
                    if (b.m_StateManage.curState.stateType == StateType.Drill) {
                        m_window.m_create.SetActive(true);
                    }
                    else {
                        m_window.m_create.SetActive(false);
                    }
                    if (b.m_StateManage.curState.stateType == StateType.Drill) {
                        var drill = b.m_StateManage.curState as BarracksDrillState;
                        m_window.m_iocnPurikura.m_iocn.sprite = drill.curSoldier._Icon;
                        m_window.m_scrollbar.value = drill._Progress;
                        if (soldierDrillNum != b.m_input.data.Execution.Count) {
                            soldierDrillNum = b.m_input.data.Execution.Count;
                            if (soldierDrillNum > 1) {
                                for (int i = 0; i < m_window.m_IconItems.Length; i++) {
                                    var ui = m_window.m_IconItems[i];
                                    if (i + 1 < soldierDrillNum) {
                                        ui.gameObject.SetActive(true);
                                        ui.m_iocn.sprite = drill.Find(b.m_input.data.Execution[i + 1]._Id)._Icon;
                                    }
                                    else {
                                        ui.gameObject.SetActive(false);
                                    }
                                }
                            }
                            else {
                                for (int i = 0; i < m_window.m_IconItems.Length; i++) {
                                    m_window.m_IconItems[i].gameObject.SetActive(false);
                                }
                            }
                        }
                    }
                    break;

                case ControllerType.Soldier: {
                        var s = SelectedObj as Soldier;
                        m_window.m_create.SetActive(false);
                        if (Input.GetMouseButtonDown(1)) {
                            s.m_input.data.TagPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                            debug.log(s.m_input.data.TagPos);
                            debug.DrawCircle(s.m_input.data.TagPos);
                        }
                    }
                    break;

                case ControllerType.SCV: {
                        //继续建造
                        if (Input.GetMouseButtonDown(1)) {
                            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                            var hit = Physics2D.CircleCast(ray.origin, 0.2f, Vector3.forward);
                            if (hit) {
                                debug.log(hit.transform.name);
                                var con = hit.transform.GetComponent<Controller>();
                                if (con) {
                                    if (con._Type == ControllerType.Barracks) {
                                        if (con.m_StateManage.curState.stateType == StateType.Build) {
                                            scv.m_StateManage.Fin<ScvBuildState>(StateType.Build).Build(con as Barracks);
                                        }
                                    }
                                }
                            }
                        }

                        //移动
                        var s = SelectedObj;
                        m_window.m_create.SetActive(false);
                        if (Input.GetMouseButtonDown(1)) {
                            s.m_input.data.IsTag = true;
                            s.m_input.data.TagPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                            debug.log(s.m_input.data.TagPos);
                            debug.DrawCircle(s.m_input.data.TagPos);
                        }
                    }
                    break;
            }
        }
    }

    private int soldierDrillNum = -1;
    public Barracks SelectInBarracks;//选取中的兵营

    public void SetInfo(Controller controller)
    {
        m_window.m_name.text = controller._Name;
        m_window.m_icon.sprite = controller._Icon;
        m_window.m_attribute.text = controller._HP + "/" + controller._MaxHP;
    }
}